home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / picture / macscrn.c < prev    next >
C/C++ Source or Header  |  1993-09-23  |  8KB  |  288 lines

  1. //    Copyright 1993 Ralph Gonzalez
  2.  
  3. /*
  4. *    FILE:        macscrn.c
  5. *    AUTHOR:        R. Gonzalez
  6. *    CREATED:    Oct. 3, 1990
  7. */
  8.  
  9. //    Functions beginning with a capital letter and variables    
  10. //    containing capitals are declared in Mac headers, and
  11. //    defined in the Macintosh Toolbox.
  12.  
  13. # include   "Dialogs.h" // TC4: #include "DialogMgr.h"
  14. # include   "Events.h"  // TC4: #include "EventMgr.h"
  15. # include   "TextEdit.h"
  16. # include   "Memory.h"  // TC4: #include "MemoryMgr.h"
  17. # include   "Menus.h"   // TC4: #include "MenuMgr.h"
  18. # include   "Quickdraw.h"
  19. # include   "Windows.h" // TC4: #include "WindowMgr.h"
  20. # include   "Fonts.h"   // TC5 only
  21. # include   "OSEvents.h"    // TC5 only
  22. # include   "LoMem.h"   // TC5 only
  23.  
  24. # define    NIL_POINTER            0L
  25. # define    MOVE_TO_FRONT        -1L
  26. # define    REMOVE_ALL_EVENTS    0
  27. # define    TITLE                "\p"
  28. # define    VISIBLE                1
  29. # define    NO_GO_AWAY            0
  30. # define    NIL_REF_CON            NIL_POINTER
  31. //    remove following line to retain menu bar.  This is recommended
  32. //    to make it easier to recover from errors when debugging!
  33. # define    NO_MBAR
  34.  
  35. # include    "macscrn.h"
  36. # include   "error.h"
  37. extern Error    *gerror;
  38.  
  39. /******************************************************************
  40. *    You must call constructor at the beginning of main().  Likely don't
  41. *    need all these initializations; most are taken from Mark &
  42. *    Reed's "Macintosh Programming Primer", Addison-Wesley, 1989
  43. ******************************************************************/
  44. Mac_Screen::Mac_Screen(void)
  45. {
  46.     double        x,
  47.                 y,
  48.                 width,
  49.                 height;
  50.     
  51.     InitGraf(&thePort);
  52.     InitFonts();
  53.     FlushEvents(everyEvent,REMOVE_ALL_EVENTS);
  54.     InitWindows();
  55.     InitMenus();
  56.     TEInit();
  57.     InitDialogs(NIL_POINTER);
  58.     InitCursor();
  59.     HideCursor();
  60.  
  61. # ifdef    NO_MBAR
  62.     old_mbar_height = MBarHeight;
  63.     MBarHeight = 0;
  64. # endif
  65.     
  66.     width = screenBits.bounds.right - screenBits.bounds.left;
  67.     height = screenBits.bounds.top - screenBits.bounds.bottom;
  68.     x = screenBits.bounds.left + width/2.;
  69.     y = screenBits.bounds.bottom + height/2.;
  70.     device_frame->set(x,y,width,height);
  71.     
  72.     normalized_frame->height =     normalized_frame->width /
  73.         get_device_aspect_ratio();
  74.         
  75.     current_window = NULL;
  76. }
  77.  
  78. /******************************************************************
  79. *    Add new window to screen
  80. ******************************************************************/
  81. int        Mac_Screen::new_window(Frame *frame)
  82. {
  83.     Rect    rectangle;
  84.     int        left,
  85.             right,
  86.             top,
  87.             bottom;
  88.     Coord2    *old_pt,
  89.             *new_pt;
  90.     Rect        mbar_rect;        // to allow window to overlap menu 
  91.     RgnHandle    mbar_rgn;
  92.     WindowPtr    temp_window;
  93.     
  94.     old_pt = new Coord2;
  95.     new_pt = new Coord2;
  96.     
  97.     old_pt->set(frame->x-frame->width/2.,frame->y-frame->height/2.);
  98.     new_pt->convert(old_pt,normalized_frame,device_frame);
  99.     left = new_pt->x;
  100.     bottom = new_pt->y;
  101.     
  102.     old_pt->set(frame->x+frame->width/2.,frame->y+frame->height/2.);
  103.     new_pt->convert(old_pt,normalized_frame,device_frame);
  104.     right = new_pt->x;
  105.     top = new_pt->y;
  106.     
  107.     delete old_pt;
  108.     delete new_pt;
  109.     
  110.     SetRect(&rectangle,left,top,right,bottom);
  111.     
  112. // TC4:     temp_window = NewWindow(NIL_POINTER,&rectangle,TITLE,
  113. //      VISIBLE,plainDBox,MOVE_TO_FRONT,NO_GO_AWAY,NIL_REF_CON);
  114.  
  115.     temp_window = NewWindow(NIL_POINTER,&rectangle,TITLE,
  116.         VISIBLE,plainDBox,(WindowPtr) MOVE_TO_FRONT,NO_GO_AWAY,NIL_REF_CON);
  117.  
  118. # ifdef    NO_MBAR
  119.     if (top < screenBits.bounds.top+old_mbar_height)
  120.     {
  121.         if (bottom < screenBits.bounds.top+old_mbar_height)
  122.             SetRect(&mbar_rect,0,0,right-left,bottom-top);
  123.         else
  124.             SetRect(&mbar_rect,0,0,right-left,
  125.                 screenBits.bounds.top+old_mbar_height-top);
  126.         mbar_rgn = NewRgn();
  127.         RectRgn(mbar_rgn,&mbar_rect);
  128.         UnionRgn(temp_window->visRgn,mbar_rgn,temp_window->visRgn);
  129.         DisposeRgn(mbar_rgn);
  130.     }
  131. # endif
  132.  
  133.     if (num_windows < MAX_WINDOWS)
  134.     {
  135.         window[num_windows++] = temp_window;
  136.         set_current_window(num_windows-1);
  137.         return num_windows-1;
  138.     }
  139.     else
  140.     {
  141.         gerror->report("Ran out of windows");
  142.         DisposeWindow(temp_window);
  143.         return num_windows-1;
  144.     }
  145. }
  146.  
  147. /******************************************************************
  148. *    Bring window to front.
  149. ******************************************************************/
  150. void    Mac_Screen::make_closest(int window_num)
  151. {
  152.     if (window_num > -1 && window_num < num_windows)
  153.         SelectWindow(window[window_num]);
  154.     else
  155.         gerror->report("Illegal window number");
  156. }
  157.  
  158. /******************************************************************
  159. *    Get coordinate frame of window in device coordinates.
  160. ******************************************************************/
  161. void    Mac_Screen::get_window_device_frame(int window_num,
  162.                                             Frame *frame)
  163. {
  164.     double    x,
  165.             y,
  166.             width,
  167.             height;
  168.             
  169.     if (window_num > -1 && window_num < num_windows)
  170.     {
  171.         width =        window[window_num]->portRect.right -
  172.                     window[window_num]->portRect.left;
  173.         height =    window[window_num]->portRect.top -
  174.                     window[window_num]->portRect.bottom;
  175.         x =         window[window_num]->portRect.left + width/2.;
  176.         y =         window[window_num]->portRect.bottom + height/2.;
  177.         frame->set(x,y,width,height);
  178.     }
  179.     else
  180.         gerror->report("Illegal window number");
  181. }
  182.  
  183. /******************************************************************
  184. *    Sets the current drawing window.
  185. ******************************************************************/
  186. void    Mac_Screen::set_current_window(int window_num)
  187. {
  188.     if (window_num > -1 && window_num < num_windows)
  189.     {
  190.         current_window = window[window_num];
  191.         SetPort(current_window);
  192.     }
  193.     else
  194.         gerror->report("Illegal window number");
  195. }
  196.  
  197. /******************************************************************
  198. *    sets the current drawing color.  The colors are defined
  199. *    in "Quickdraw.h".  Call set_current_window() first!
  200. ******************************************************************/
  201. void    Mac_Screen::set_pen_color(color x)
  202. {
  203.     if (current_window != NULL)
  204.         switch ((int) x)
  205.         {
  206.             case 0:        ForeColor(blackColor);
  207.                         break;
  208.             case 1:        ForeColor(whiteColor);
  209.                         break;
  210.             case 2:        ForeColor(redColor);
  211.                         break;
  212.             case 3:        ForeColor(yellowColor);
  213.                         break;
  214.             case 4:        ForeColor(greenColor);
  215.                         break;
  216.             case 5:        ForeColor(blueColor);
  217.                         break;
  218.             case 6:        ForeColor(cyanColor);
  219.                         break;
  220.             case 7:        ForeColor(magentaColor);
  221.                         break;
  222.             default:    break;
  223.         }
  224.     else
  225.         gerror->report("Can't set color with no windows");
  226. }
  227.  
  228. /******************************************************************
  229. *    calls the appropriate Mac Toolbox function to make the window
  230. *    the current color.   Call set_current_window() first!
  231. ******************************************************************/
  232. void    Mac_Screen::fill_window(void)
  233. {
  234.     if (current_window != NULL)
  235.         FillRect(&(current_window->portRect),black);
  236.     else
  237.         gerror->report("Can't fill window with no windows");
  238. }
  239.  
  240. /******************************************************************
  241. *    Move present pen position to new position using device
  242. *    coordinates.  Call set_current_window() first!
  243. ******************************************************************/
  244. void    Mac_Screen::move_to(Coord2* c)
  245. {
  246.     if (current_window != NULL)
  247.         MoveTo((int) c->x,(int) c->y);
  248.     else
  249.         gerror->report("Can't move_to() with no windows");
  250. }
  251.  
  252. /******************************************************************
  253. *    Draw from present pen position to new position using device
  254. *    coordinates.  Call set_current_window() first!
  255. ******************************************************************/
  256. void    Mac_Screen::draw_to(Coord2* c)
  257. {
  258.     if (current_window != NULL)
  259.         LineTo((int) c->x,(int) c->y);
  260.     else
  261.         gerror->report("Can't draw_to() with no windows");
  262. }
  263.  
  264. /******************************************************************
  265. *    mouse_button_is_down() checks whether the mouse button is down,
  266. *    returns TRUE if so, FALSE if not.
  267. ******************************************************************/
  268. boolean    Mac_Screen::mouse_button_is_down(void)
  269. {
  270.     return Button();
  271. }
  272.  
  273. /******************************************************************
  274. *    Destroy screen.
  275. ******************************************************************/
  276. Mac_Screen::~Mac_Screen(void)
  277. {
  278.     int        window_num;
  279.     
  280.     for (window_num=0 ; window_num<num_windows ; window_num++)
  281.         DisposeWindow(window[window_num]);
  282.  
  283. # ifdef    NO_MBAR
  284.     MBarHeight = old_mbar_height;
  285. # endif
  286. }
  287.  
  288.